<template>
  <div class="ele-body">
    <el-card shadow="never">
      
      <!-- 搜索表单 -->
      <el-popover ref="searchClass" placement="bottom" offset="0" @hide="clearForm" :width="300">
        <template>
          <el-form :model="table.where" label-width="77px" class="ele-form-search"
                   @keyup.enter.native="$refs.classTable.reload()" @submit.native.prevent>
            <el-row>
              <el-col>
                <el-form-item label="班次名称:">
                  <el-input v-model="table.where.name" placeholder="请输入班次名称" clearable/>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="班次名称:">
                  <el-input v-model="table.where.name" placeholder="请输入班次名称" clearable/>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="班次名称:">
                  <el-input v-model="table.where.name" placeholder="请输入班次名称" clearable/>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item>
                  <div class="ele-form-actions">
                    <el-button type="primary" @click="$refs.classTable.reload()" icon="el-icon-search" class="ele-btn-icon">查询
                    </el-button>
                    <el-button @click="(table.where={})&&$refs.classTable.reload()">重置</el-button>
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </el-popover>

      <!-- 操作按钮 -->
      <div class="ele-table-tool ele-table-tool-default">
        <el-button v-popover:searchClass type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">查询</el-button>
        <el-button @click="showClassEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">新增班次</el-button>
        <el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">批量删除
        </el-button>
      </div>
      
      <!-- 数据表格 -->
      <ele-data-table ref="classTable" :config="table" :choose.sync="choose" height="calc(100vh - 315px)" highlight-current-row>
        <template slot-scope="{index}">
          <el-table-column type="selection" width="45" align="center" fixed="left"/>
          <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" show-overflow-tooltip/>
                  
          <el-table-column prop="name" label="班次名称" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="attend_period" label="考勤时段" sortable="custom" show-overflow-tooltip min-width="120"/>
                                
          <el-table-column prop="note" label="备注" sortable="custom" show-overflow-tooltip min-width="120"/>

          <el-table-column label="操作" width="130px" align="center" :resizable="false"  fixed="right">
            <template slot-scope="{row}">
              <el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
              <el-popconfirm title="确定要删除此考勤班次吗？" @confirm="remove(row)" class="ele-action">
                <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>
    
    <!-- 编辑弹窗 -->
    <el-dialog :title="editForm.id?'修改考勤班次':'新增考勤班次'" :visible.sync="showClassEdit" width="800px"
               @closed="editForm={period: 'oneOfOne', classPeriod: [{},{},{}]}" :destroy-on-close="false" :lock-scroll="false">

      <el-form :model="editForm" ref="editClassForm" :rules="editRules" label-width="100px">
                          
        <el-form-item label="班次名称：" prop="name">
            <el-input v-model="editForm.name" placeholder="请输入班次名称" clearable/>
        </el-form-item>
        <!--要使用el-tab就需要设置destroy-on-close="false"-->
        <el-form-item label="考勤时段：" prop="period">
          <el-tabs v-model="editForm.period" @tab-click="handleTabClick">
            <el-tab-pane label="一天一次考勤" name="oneOfOne">
              <div v-for="(period, index) in editForm.classPeriod.slice(0,1)" v-bind:key="index">
                <el-row class="el-row" v-if="editForm.period === 'oneOfOne'">
                  <el-col :span="1">
                    <span>{{index + 1}}</span>
                  </el-col>
                  <el-col :span="23">
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="上学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                        required: true, message: '上学时间不能为空', trigger: 'blur'
                                      }"
                        >
                          <el-time-picker
                              v-model="period.goToSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                            }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signInTime'"
                            :rules="{
                               required: true, message: '可签到时间不能为空', trigger: 'blur'
                            }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signInTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                              start : '00:00',
                              end : '23:59',
                              step : '00:01'
                            }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签到
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="放学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                        required: true, message: '放学时间不能为空', trigger: 'blur'
                                      }"
                        >
                          <el-time-picker
                              v-model="period.leaveSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                            }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signOutTime'"
                            :rules="{
                               required: true, message: '可签退时间不能为空', trigger: 'blur'
                            }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signOutTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                              start : '00:00',
                              end : '23:59',
                              step : '00:01'
                            }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签退
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
            </el-tab-pane>
            <el-tab-pane label="一天两次考勤" name="twoOfOne">
              <div v-for="(period, index) in editForm.classPeriod.slice(0,2)" v-bind:key="index">
                <el-row class="el-row" v-if="editForm.period === 'twoOfOne'">
                  <el-col :span="1">
                    <span>{{index + 1}}</span>
                  </el-col>
                  <el-col :span="23">
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="上学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                      required: true, message: '上学时间不能为空', trigger: 'blur'
                                    }"
                        >
                          <el-time-picker
                              v-model="period.goToSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                            format: 'HH:mm',
                          }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signInTime'"
                            :rules="{
                             required: true, message: '可签到时间不能为空', trigger: 'blur'
                          }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signInTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                            format: 'HH:mm',
                            start : '00:00',
                            end : '23:59',
                            step : '00:01'
                          }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签到
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="放学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                      required: true, message: '放学时间不能为空', trigger: 'blur'
                                    }"
                        >
                          <el-time-picker
                              v-model="period.leaveSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                            format: 'HH:mm',
                          }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signOutTime'"
                            :rules="{
                             required: true, message: '可签退时间不能为空', trigger: 'blur'
                          }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signOutTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                            format: 'HH:mm',
                            start : '00:00',
                            end : '23:59',
                            step : '00:01'
                          }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签退
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
            </el-tab-pane>
            <el-tab-pane label="一天三次考勤" name="threeOfOne">
              <div v-for="(period, index) in editForm.classPeriod.slice(0,3)" v-bind:key="index">
                <el-row class="el-row" v-if="editForm.period === 'threeOfOne'">
                  <el-col :span="1">
                    <span>{{index + 1}}</span>
                  </el-col>
                  <el-col :span="23">
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="上学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                        required: true, message: '上学时间不能为空', trigger: 'blur'
                                      }"
                        >
                          <el-time-picker
                              v-model="period.goToSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                            }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signInTime'"
                            :rules="{
                               required: true, message: '可签到时间不能为空', trigger: 'blur'
                            }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signInTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                              start : '00:00',
                              end : '23:59',
                              step : '00:01'
                            }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签到
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row>
                      <el-col :span="10">
                        <el-form-item label="放学时间"
                                      :key="period.key"
                                      :prop="'classPeriod.' + index + '.goToSchoolTime'"
                                      :rules="{
                                        required: true, message: '放学时间不能为空', trigger: 'blur'
                                      }"
                        >
                          <el-time-picker
                              v-model="period.leaveSchoolTime"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                            }"
                              placeholder="选择时间">
                          </el-time-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :span="14">
                        <el-form-item
                            :key="period.key"
                            :prop="'classPeriod.' + index + '.signOutTime'"
                            :rules="{
                               required: true, message: '可签退时间不能为空', trigger: 'blur'
                            }"
                        >
                          <el-time-picker
                              is-range
                              v-model="period.signOutTime"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              placeholder="选择时间范围"
                              value-format="HH:mm"
                              :picker-options="{
                              format: 'HH:mm',
                              start : '00:00',
                              end : '23:59',
                              step : '00:01'
                            }"
                          >
                          </el-time-picker>&nbsp;&nbsp;可签退
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </div>
            </el-tab-pane>



          </el-tabs>
        </el-form-item>
                                                  
        <el-form-item label="" prop="max_late_min">
            <el-input v-model="editForm.max_late_min" placeholder="请输入迟到多少分钟算正常" clearable>
              <template slot="prepend">迟到</template>
              <template slot="append">分算正常</template>
            </el-input>
        </el-form-item>
                                                      
        <el-form-item label="" prop="max_early_min">
          <el-input v-model="editForm.max_early_min" placeholder="请输入早退多少分钟算正常" clearable>
            <template slot="prepend">早退</template>
            <template slot="append">分算正常</template>
          </el-input>
        </el-form-item>
                                                      
        <el-form-item label="" prop="absent_min">
          <el-input v-model="editForm.absent_min" placeholder="请输入迟到多少分钟算旷工" clearable>
            <template slot="prepend">迟到</template>
            <template slot="append">分算旷工</template>
          </el-input>
        </el-form-item>

        <el-form-item label="备注" prop="note">
          <el-input type="textarea"
                    :rows="2" v-model="editForm.note" placeholder="请输入备注" clearable/>
        </el-form-item>
                              
      </el-form>

      <div slot="footer">
        <el-button @click="showClassEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
export default {
  name: "attendClass",
  data() {
    return {
      table: {url: '/attendClass/list', where: {}},  // 表格配置
      choose: [],  // 表格选中数据
      showClassEdit: false,  // 是否显示表单弹窗
      editForm: {
        period : '',
        classPeriod : [{},{},{}], // 班段
      },  // 表单数据
      editRules: {  // 表单验证规则
                        
        name: [
          {required: true, message: '请输入班次名称', trigger: 'blur'}
        ],
                          
        max_late_min: [
          {required: true, message: '请输入迟到多少分钟算正常', trigger: 'blur'}
        ],
                          
        max_early_min: [
          {required: true, message: '请输入早退多少分钟算正常', trigger: 'blur'}
        ],
                          
        absent_min: [
          {required: true, message: '请输入迟到多少分钟算旷工', trigger: 'blur'}
        ],
              
      },
    }
  },
  created() {
    this.editForm.period = 'oneOfOne'; // 默认选中一天一次考勤
  },
  methods: {
    /**
     * 显示编辑
     */
    edit(row) {
      this.$http.get('/attendClass/info?id=' + row.id).then(res => {
          // TODO 这里应该返回班段信息
          this.editForm = res.data;
          this.showClassEdit = true;
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 保存编辑
     */
    save() {
      this.$refs['editClassForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({lock: true});
          this.$http.post('/attendClass/edit', this.editForm).then(res => {
            loading.close();
            this.showClassEdit = false;
            this.$message({type: 'success', message: res.msg});
            this.$refs.classTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 刪除(批量刪除)
     */
    remove(row) {
      console.log(row)
      if (!row) {  // 批量删除
        if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
        let ids = this.choose.map(d => d.id);
        this.$confirm('确定要删除选中的考勤班次吗?', '提示', {type: 'warning'}).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/attendClass/destroy', {id: ids}).then(res => {
            loading.close();
            this.$message({type: 'success', message: res.msg});
            this.$refs.classTable.reload();
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => 0);
      } else {  // 单个删除
        const loading = this.$loading({lock: true});
        this.$http.post('/attendClass/destroy', {id:row.id}).then(res => {
          loading.close();
          this.$message({type: 'success', message: res.msg});
          this.$refs.classTable.reload();
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }
    },
    /**
     * 清理查询表单
     */
    clearForm() {
      this.where = {};
    },
    handleTabClick() {

    }
  }
}
</script>

<style scoped>
.el-row {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap
}
.ele-block >>> .el-upload, .ele-block >>> .el-upload-dragger {
  width: 100%;
}
>>> .el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 150px;
}
>>> .el-date-editor--timerange.el-input, .el-date-editor--timerange.el-input__inner {
  width: 300px;
}
</style>